﻿// -----------------------------------------------------------------------
// <copyright file="UrlPathMapping.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace NetFramework.Web.Configuration
{
    using System;
    using NetFramework.Collection;
    using System.Xml.Serialization;

    /// <summary>
    /// <typeparamref name="SitePathInfo"/> 类是站点的名称、路径、描述等基本信息的表示形式
    /// </summary>
    /// <remarks>站点可以是根站点也可以是虚拟目录</remarks>
    public class UrlPathMapping : IKeyedObject
    {
        private string name = "";
        private string description = "";
        private string[] paths;
        private Uri[] webSiteUri;

        /// <summary>
        /// 获取 <typeparamref name="SitePathInfo"/> 的Key键。
        /// </summary>
        public string Key
        {
            get { return name; }
        }

        /// <summary>
        /// 获取或设置当前站点的名称。
        /// </summary>
        /// <value>当前站点的名称。</value>
        /// <remarks>站点的名称在站点配置中不允许重复。</remarks>
        [XmlAttribute("name")]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// url paths
        /// </summary>
        [XmlArray("paths")]
        [XmlArrayItem("path")]
        public string[] Paths
        {
            get
            {
                return paths;
            }
            set
            {
                paths = value;

                if (paths != null)
                {
                    for (int i = 0; i < paths.Length; i++)
                    {
                        paths[i] = paths[i].TrimEnd('/', '\\');
                    }

                    CreateUri();
                }
            }
        }

        /// <summary>
        /// 创建Uri
        /// </summary>
        public void CreateUri()
        {
            if (this.paths != null && this.paths.Length > 0)
            {
                this.webSiteUri = new Uri[this.paths.Length];
                for (int i = 0; i < this.paths.Length; i++)
                {
                    this.webSiteUri[i] = new Uri(this.paths[i]);
                }
            }
        }
    }
}
